package com.tedu.spingboot2307.controller;

import com.tedu.spingboot2307.entity.Articl;
import com.tedu.spingboot2307.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    //表示保存所有用户信息数据的目录:articles
    private static File Ar;
    static {
        Ar=new File("articles");
        if (!Ar.exists()){
            Ar.mkdirs();
        }
    }

    @RequestMapping("/writeArticle")
    public void write(HttpServletRequest request, HttpServletResponse response){
        //1.获取表单信息
        System.out.println("开始处理发表内容");
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String content=request.getParameter("content");
        System.out.println(title+","+author+","+content);
        /*
            验证
         */
        if (title==null||title.isEmpty()||
            author==null||author.isEmpty()||
            content==null||content.isEmpty()){
            /*
                失败
             */
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        File file=new File(Ar,title+".obj");

        if (file.exists()){//判断是否用户名一样
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        //序列化
        Articl articl=new Articl(title,author,content);
        try (
                FileOutputStream fos=new FileOutputStream(file);
                ObjectOutputStream oos=new ObjectOutputStream(fos);
                ){
            oos.writeObject(articl);
            response.sendRedirect("article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @RequestMapping("/write")
    public void writeA(HttpServletRequest request, HttpServletResponse response) {
        //1将user目录下所有的注册用户读取出来
        System.out.println("开始处理文章页面");
        List<Articl> articlList=new ArrayList<>();
        //获取users目录下所有obj文件
        File[] subs=Ar.listFiles(f->f.getName().endsWith("obj"));
        for (File sub:subs){
            try (
                    FileInputStream fis=new FileInputStream(sub);
                    ObjectInputStream ois=new ObjectInputStream(fis);
                    ){
                Articl artic=(Articl) ois.readObject();
                articlList.add(artic);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        for (Articl ar:articlList){
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head");
            pw.println("<meta charset=\"utf-8\">");
            pw.println("<title>发表文章</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>发表文章</h1>");
            pw.println("<table border=\"1\">");

            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td align=\"center\">"+ar.getTitlr()+"</td>");
            pw.println("</tr>");

            pw.println("<tr>");
            pw.println("<td>作者</td>");
            pw.println("<td align=\"center\">"+ar.getAuthor()+"</td>");
            pw.println("</tr>");

            pw.println("<tr>");
            pw.println("<td>内容</td>");
            pw.println("<td colspan=\"2\"><textarea cols=\"50\" rows=\"20\" name=\"content\">" + ar.getContent() + "</textarea></td>");
            pw.println("</tr>");


            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }

        }

    }
}
